<?php
namespace Home\Controller;
use Think\Controller;
class IndexController{
    public function index(){


        //$this->display();
    }
    public function doPost(){
        $headers = getallheaders();
        $postHeader = array('appId:'.$headers['appId'],'appKey:'.$headers['appKey'],'token:'.$headers['token'],'Content-Type: application/json; charset=utf-8');
        $postUrl = $headers['url'];
        $postData = $_POST;
        if (count($postData) == 0){
            $postData = '{}';
        }else{
            $postData = json_encode($postData);
        }
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, true);//设置header
        curl_setopt($ch, CURLOPT_HTTPHEADER, $postHeader);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        //获得响应结果里的：头大小
        list($header, $body) = explode("\r\n\r\n", $data, 2);
        $token = null;
        $headArr = explode("\r\n", $header);

        foreach ($headArr as $loop) {
            if (strpos($loop, "token") !== false) {
                $token = trim(substr($loop, 7));
            }
        }
        header("Content-Type: application/json; charset=utf-8");
        header("token:".$token );
        echo json_encode($body);
    }
    public function getJsonModel(){
        require ('McmModel.php');
        $app = $_POST['app'];
        $model = $_POST['model'];
        $cacheName = $app.'_'.$model;
        if(S($cacheName)){
            echo S($cacheName);//"{'a':'a'}";
        }else{
            $mcm = new McmModel();
            $name='model';
            $filter='{"where":{"app":"'.$app.'","model":"'.$model.'"},"limit":10}';
            $jsonModel = $mcm->objFindAll($name,$filter);
            S($cacheName,$jsonModel,72000);
            echo S($cacheName);
        }
//        $mcm = new McmModel();
//        $name='model';
//        $filter='{"where":{"app":"'.$app.'","model":"'.$model.'"},"limit":10}';
//        $jsonModel = $mcm->objFindAll($name,$filter);
//        echo $jsonModel;

    }
    public function getAllJsonModel(){
        require ('McmModel.php');
        $mcm = new McmModel();
        $name='model';
        $filter='{"limit":100}';
        $jsonModel = $mcm->objFindAll($name,$filter);
        echo $jsonModel;
    }
}